import requests
from bs4 import BeautifulSoup
import csv
import json

url = "http://data.sports.sohu.com/nba/nba_players_rank.php?order_by=points&spm=smpc.fb-nba-home.top-dc.2.1620824904040nf6byr7"
file_type = "csv"

res = requests.get(url)
html = res.content

soup = BeautifulSoup(html, 'html.parser')

rows = soup.find_all('tr')

data_list = []

for row in rows[1:51]:
    row_values = []
    for cell in row.find_all(['td', 'th']):
        row_values.append(cell.text.strip())
    data_list.append(row_values)

if file_type == "json":
    with open("nba_players.json", "w") as fp:
        fp.write(json.dumps(data_list))
else:
    with open("nba_players.csv", "w") as fp:
        writer = csv.writer(fp)
        writer.writerows(data_list)
